Kwik.Page.include("Kwik.Ui.Controls.Control.js");
Kwik.Page.include("Kwik.Collections.Hashtable.js");
Kwik.Page.include("Kwik.Ui.Controls.ToolbarButton.js");
Kwik.Page.include("Kwik.Dom.Utility.js");

/** 
* A toolbar which can be used to support other controls with user events
* @constructor
* @extends Kwik.Ui.Controls.Control
* @requires Kwik.Ui.Controls.Control
* @requires Kwik.Ui.Controls.ToolbarButton
* @requires Kwik.Collections.Hashtable
* @requires Kwik.Dom.Utility
*/
Kwik.Ui.Controls.Toolbar = function(config)
{
	/** @ignore */
	Kwik.Utility.extend(this, "Kwik.Ui.Controls.Control");
	this.__class.push("Kwik.Ui.Controls.Toolbar");

	/** Hastable used to store buttons by name @type Kwik.Collections.Hashtable */
	this._table = new Kwik.Collections.Hashtable();

	/** Add a button to the toolbar, either a name value pair or Kwik.Ui.Controls.ToolbarButton is expected as parameter @type Kwik.Ui.Controls.ToolbarButton */
	this.add = function Toolbar_add()
	{
		var h = this.getHeight()-2;
		var button = null;
		if(arguments.length == 1 && Kwik.Type.isKwikType(arguments[0], "Kwik.Ui.Controls.ToolbarButton"))
		{
			button = arguments[0];
		}
		else
		{
			if(arguments.length == 1 && Kwik.Type.isElement(arguments[0]))
				button = new Kwik.Ui.Controls.ToolbarButton({element: arguments[0]});
			
			if(arguments.length == 1 && typeof arguments[0].name != "undefined")
				button = new Kwik.Ui.Controls.ToolbarButton(arguments[0]);

			if(arguments.length == 2)
				button = new Kwik.Ui.Controls.ToolbarButton({name: arguments[0], value: arguments[1], width:h, height:h});
		}
		
		Kwik.Page.log.write("Kwik.Ui.Controls.Toolbar: Added button to toolbar with name '" + button.getName() + "'.");
		this._table.add(button.getName(), button);

		if(this.element !== button.element.parentNode)
			this.element.appendChild(button.element);

		button.attachEvent("onclick", new Function(this.referenceString() + ".fireEvent(\"onbuttonclick\", [this]);"));
		button.attachEvent("onmousedown", new Function(this.referenceString() + ".fireEvent(\"onbuttonmousedown\", [this]);"));
		button.attachEvent("onmouseup", new Function(this.referenceString() + ".fireEvent(\"onbuttonmouseup\", [this]);"));
		return button;
	};

	/** Remove a button from the toolbar @type Kwik.Ui.Controls.ToolbarButton */
	this.remove = function Toolbar_remove(name)
	{
		if(!this._table.containsKey(name)){return null;}
		this._table[name].destroy();
		return this._table.remove(name);
	};

	/** Get all buttons from the toolbar in an array @type Kwik.Ui.Controls.ToolbarButton[] */
	this.toArray = function()
	{
		this._table.toArray();
	};

	/** Load the toolbar from an element, tries to add buttons from child elements and prevents the loader from evaluating child nodes */
	this.load = function Toolbar_load(element)
	{
		var list = Kwik.Dom.Utility.getChildElements(element, "div");
		while(element.childNodes.length>0){element.removeChild(element.childNodes[0]);}

		if(element.parentNode!=null)
		{
			Kwik.Page.log.write("Kwik.Ui.Controls.Toolbar.load: Replacing element with control element.");
			Kwik.Ui.Utility.copyAttributes(element, this.element);
			element.parentNode.replaceChild(this.element, element);
		}

		for(var i=0; i<list.length; i++)
		{
			this.add(list[i]);
		}
		return null;
	};

	/** Event fired when a toolbar button is clicked */
	this.onbuttonclick = function(button){};
	/** Event fired when a mouse button is pressed on the toolbar button */
	this.onbuttonmousedown = function(button){};
	/** Event fired when a mouse button is released on the toolbar button */
	this.onbuttonmouseup = function(button){};

	/** @ignore */
	this.main = function Toolbar_main(config)
	{
		if(config){this.configure(config);}
		this.create();
	}; this.main(config);
};